package com.wgl.rocket.config;

import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName RocketConfig.java
 * @Description TODO
 * @createTime 2022年02月14日 14:23:00
 */
@Configuration
public class RocketConfig {
    @Value("${rocketmq.nameserver}")
    private String nameserver;
    @Value("${rocketmq.producer.group}")
    private String group;

    @Bean
    public DefaultMQProducer getDefaultMQProducer(){
        //name 为Producer Group ID 设置 生产者组id
        DefaultMQProducer producer = new DefaultMQProducer(group);

//指定NameServer的ip:端口
        producer.setNamesrvAddr(nameserver);
//设置发送消息失败后的重试次数 默认为2次 同步发送
        producer.setRetryTimesWhenSendFailed(3);
        //设置发送消息失败后的重试次数 默认为2次 异步发送
        producer.setRetryTimesWhenSendAsyncFailed(0);
        //设置发送超时时限为5s  默认3s  参数单位为毫秒
        producer.setSendMsgTimeout(5000);
        //设置队列数量为2 默认为4
        producer.setDefaultTopicQueueNums(2);
//开启生产者
        try {
            producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        return producer;
    }

}
